const child_process = require('child_process');

const Rec = require('../models/Recommend')

const recommender = {
    update: async function (req) {
        const uid = req.session.uid;
        var workerProcess = child_process.exec('/usr/local/python3/bin/python3 script/knn.py ' + uid, 
        async function (error, stdout, stderr) {
            if (error) {
                console.log(error.stack);
                console.log('Error code: ' + error.code);
                console.log('Signal received: ' + error.signal);
            }
            console.log('stdout: ', stdout);
            var mids_str = stdout.split('\n')[0]; // mid裂变
            var time_str = stdout.split('\n')[1];

            var state = await Rec.isNewUser(uid)
            console.log("state",state, uid, mids_str)
            if(state == undefined){
                var res = await Rec.insert(uid, mids_str);
                console.log("res1", res)
            }else{
                var res = await Rec.update(uid, mids_str);
                console.log("res2", res)
            }
            console.log("Recommender cost_time: ", time_str);
        });
        workerProcess.on('exit', function (code) {
        });
    }
}



module.exports = recommender;